function Wupload(url,_that,postdata){
	var xhr = new XMLHttpRequest();
	var postdata = postdata || {};

	const LENGTH = 1024 * 1024*1;
	
	this.start = 0;
	this.end = this.start + LENGTH;
	this.status = 0;
	this.call = '';
	this.files = _that.files;
	this.progress_call = null;
	
	
	//对外方法，传入文件对象
	this.init = function(_call){
		var _call = _call || '';
		this.call = _call;
		this.status = 1;
		this.blob_num = 0;

		var file = this.files[0];
		var blob = this.cutFile(file);
		if(blob){
			this.sendFile(blob,file);
		}
		

	}
	this.start_task = function(){
		this.status = 1;
		var file = this.files[0];
		var blob = this.cutFile(file);
		if(blob){
			this.sendFile(blob,file);
		}
	}
	//停止文件上传
	this.stop_task = function(){
		xhr.abort();
		this.status = 0;
		console.log('stop');
	}
	//切割文件
	this.cutFile = function(file){
		if(this.status==0){
			typeof(this.call)=='function' && this.call(2);
			return false;
		}
		
		
		var file_blob = file.slice(this.start,this.end);
		this.start = this.end;
		this.end = this.start + LENGTH;
		this.blob_num  += 1;
		return file_blob;
	};
	this.onProgress = function(call){
		this.progress_call = call;
		
	}
	//发送文件
	this.sendFile = function (blob,file){
		var self = this;

		var total_blob_num = Math.ceil(file.size / LENGTH);
		var form_data = new FormData();
		
		form_data.append('file',blob);
		form_data.append('blob_num',this.blob_num);
		form_data.append('total_blob_num',total_blob_num);
		form_data.append('file_name',file.name);
		form_data.append('size',file.size);
		
		for(var i in postdata){
			form_data.append(i,postdata[i]);
		}

		
		
		xhr.open('POST',url,false);
		xhr.onreadystatechange  = function () {
			
			var progress,x_status,result;
			if(total_blob_num == 1){
				progress = 100;
			}else{
				progress = parseInt(Math.min(100,(self.blob_num/total_blob_num)* 100 ));
			}
			x_status = 1;

			if (xhr.readyState==4 && xhr.status==200){
				
				var t = window.setTimeout(function(){
					if(self.start < file.size){
						var ls_blob = self.cutFile(file);
						if(ls_blob){
							self.sendFile(ls_blob,file);
						}
						
						
					}else{
						window.clearTimeout(t);
					}
				},30);
				x_status = 0;
				try{
					result = eval('('+xhr.responseText+')');
					result.filesize = file.size;
				}
				catch(e){
					console.log(e);
					return;
				}
				if(result.data.code==0){
					typeof(self.call)=='function' && self.call(result.data);
				}
				
				typeof(self.progress_call)=='function' && self.progress_call(status,progress,result);
			}

			
		}
		xhr.send(form_data);
	}
}
export default Wupload;